package com.jye.cloud.user.controller;



import com.jye.cloud.Constants.MyConstants;
import com.jye.cloud.domain.Payment;
import com.jye.cloud.user.service.CoursesService;
import com.jye.cloud.user.service.PaymentService;
import com.jye.cloud.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pay")
@Api(tags="支付控制器API")
public class PaymentController {

    @Autowired
    PaymentService paymentService;

    @Autowired
    CoursesService coursesService;

    @GetMapping("/findPayById")
    @ApiOperation(value="查找支付信息", notes="根据id查找支付信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "订单ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findPayById(int id){
        JsonResult jsonResult = new JsonResult();
        Payment payment = paymentService.findPayById(id);
        if(!StringUtils.isEmpty(payment)){
            jsonResult.setData(payment);
        }else{
            jsonResult.setMsg("抱歉!无此订单!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping("/findMyPayByUId")
    @ApiOperation(value="查找我的订单", notes="根据uId查找订单", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "skip",
                    value = "跳过几条数据", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "limit",
                    value = "显示几条数据", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyPayByUId(int uId,int skip,int limit){
        JsonResult jsonResult = new JsonResult();
        List<Payment> paymentList = paymentService.findMyPayByUId(uId,skip,limit);
        if(paymentList.size()!=0){
            jsonResult.setData(paymentList);
        }else{
            jsonResult.setMsg("抱歉!你暂无订单!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping("/findMyPayByUIdAndStatus")
    @ApiOperation(value="查找我的订单按状态分类", notes="根据uId和status查找订单", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "status",
                    value = "用户id", required = true,
                    dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "skip",
                    value = "跳过几条数据", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "limit",
                    value = "显示几条数据", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyPayByUIdAndStatus(int uId,String status,int skip,int limit){
        JsonResult jsonResult = new JsonResult();
        List<Payment> paymentList = paymentService.findMyPayByUIdAndStatus(uId,status,skip,limit);
        if(paymentList.size()!=0){
            jsonResult.setData(paymentList);
        }else{
            jsonResult.setMsg("抱歉!你暂无分类为["+status+"]的订单!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @PostMapping("/addPay")
    @ApiOperation(value="添加订单消息", notes="用户id:uId,交易编号:pay_trade,总价格:total_price,课程id:orderId", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "payment",
                    value = "支付订单实体类", required = true,
                    dataType = "Payment"),
    })
    public JsonResult addPay(Payment payment,BindingResult bindingResult){
        JsonResult jsonResult = new JsonResult();
        System.out.println("add:"+payment);
        //寻找下有无记录
        Payment ispay = paymentService.findPayByUIdAndCId(payment.getUId(), payment.getOrderId());
        if( StringUtils.isEmpty(ispay) ){
            //无记录
            int i = paymentService.addPay(payment);
            if(i!=0){
                //添加成功
                Payment pay = paymentService.findPayByUIdAndCId(payment.getUId(), payment.getOrderId());
                jsonResult.setData(pay.getId());
            }else{
                //添加失败
                jsonResult.setCode("500");
                jsonResult.setMsg("添加失败");
            }
        }else{
            //有记录
            jsonResult.setData(ispay.getId());
            jsonResult.setMsg("已存在该订单!请勿重复支付!");
        }

        return jsonResult;
    }



    @GetMapping("/findPayIdByUIdAndCId")
    @ApiOperation(value="查找刚才支付订单", notes="根据用户uId,课程cId来查找刚才支付订单", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "cId",
                    value = "课程id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findPayIdByUIdAndCId(int uId,int cId){
        JsonResult jsonResult = new JsonResult();
        Payment payment = paymentService.findPayByUIdAndCId(uId,cId);
        if( StringUtils.isEmpty(payment) ){
            jsonResult.setCode("500");
            jsonResult.setMsg("抱歉,未找到该订单,请确认你已下单!");
        }else{
            String paid = MyConstants.payStatus.paid;
            if(payment.getStatus().equals(paid)){
                //已支付
                jsonResult.setData(payment.getId());
            }else{
                jsonResult.setCode("500");
                jsonResult.setMsg("抱歉,您未支付,请支付!");
            }
        }

        return jsonResult;
    }

    @PutMapping("/confrimPayById")
    @ApiOperation(value="确认订单消息", notes="根据id确认订单消息", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "订单ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult confrimPayById(int id){
        JsonResult jsonResult = new JsonResult();
        String msg = paymentService.confrimPayById(id);
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    @PutMapping("/cancelPayById")
    @ApiOperation(value="取消订单消息", notes="根据id取消订单消息", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "订单ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult concelPayById(int id){
        JsonResult jsonResult = new JsonResult();
        String msg = paymentService.cancelPayById(id);
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    @PutMapping("/paidPayById")
    @ApiOperation(value="确认支付订单", notes="根据id确认支付订单", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "订单ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult paidPayById(int id){
        JsonResult jsonResult = new JsonResult();
        int code = paymentService.paidPayById(id);
        if(code!=0){
            //支付成功
            Payment payment = paymentService.findPayById(id);
            String msg = coursesService.userBuyCourse(payment.getUId(),payment.getOrderId());
            jsonResult.setMsg(msg);
        }else {
            jsonResult.setCode("500");
            jsonResult.setMsg("支付失败!请重试!");
        }
        return jsonResult;
    }

    @PutMapping("/frozenPayById")
    @ApiOperation(value="冻结支付订单", notes="根据id冻结支付订单", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "订单ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult frozenPayById(int id){
        JsonResult jsonResult = new JsonResult();
        String msg = paymentService.frozenPayById(id);
        jsonResult.setMsg(msg);
        return jsonResult;
    }






}
